package com.jeason.fileserver.controller;

import com.jeason.fileserver.entity.FileBean;
import com.jeason.fileserver.service.FileService;
import com.jeason.fileserver.service.FileUploadService;
import com.jeason.fileserver.utils.AjaxResult;
import com.jeason.fileserver.utils.DateUtils;
import com.jeason.fileserver.utils.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 26950
 * @date 2021/1/17 11:11
 */
@Api("上传")
@RestController
@RequestMapping("/admin/upload")
public class FileUploadController {
    @Autowired
    private FileService fileService;

    @Autowired
    private FileUploadService uploadService;

    @ApiOperation("上传图片")
    @PostMapping("/img")
    public AjaxResult uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String path = uploadService.upfile(file, request);
        FileBean fileBean = new FileBean();
        Long snowflakeId = IdUtils.snowflakeId();
        fileBean.setFileId(snowflakeId);
        fileBean.setFilePath(path);
        fileService.insert(fileBean);
        return AjaxResult.success("上传成功", path);
    }
}
